﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.Common;
using System.IO;
using System.Text;
using Org.Eurekaa.DNJ.Collections;

namespace Org.Eurekaa.DNJ.UI.WebControls
{
    [ParseChildren(true, "BoxList")]
    [PersistChildren(false)]
//    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.DNJTabsControlDesigner))]
    public abstract class DNJMultiboxControl : JQueryUIControl
    {
        private DNJGenericCollection<DNJBox> _boxList = new DNJGenericCollection<DNJBox>();
        private int _currentDesignBox = 0;

        [Category("$.DNJ Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [MergableProperty(false)]
        public DNJGenericCollection<DNJBox> BoxList
        {
            get
            {
                if (_boxList == null)
                {
                    _boxList = new DNJGenericCollection<DNJBox>();
                }
                return _boxList;
            }
        }

        /// <summary>
        /// Get or set the deesign time active tab.
        /// </summary>
        [Browsable(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public int CurrentDesignBox
        {
            get { return _currentDesignBox; }
            set { _currentDesignBox = value; }
        }



        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (DesignMode)
            {
                if (_boxList.Count > 0) //FIX
                    _boxList[_currentDesignBox].Content.InstantiateIn(this);
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                
                StringBuilder sb = new StringBuilder();
                StringWriter tw = new StringWriter(sb);
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                base.RenderBeginTag(hw);
                writer.Write(sb.ToString().Replace("<span", "<div").Replace("inline-block", "block"));
            }
            else
                base.RenderBeginTag(writer);
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            if (DesignMode)
                writer.Write("</div>");
            else
                base.RenderEndTag(writer);
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
        }

        //#endregion implementations
    }
}
